/**
 * 
 */
package es.ibm.santillana.mdm.workflow.ValidarGenerales;


import java.util.ArrayList;
import java.util.Iterator;

import com.ibm.pim.catalog.Catalog;
import com.ibm.pim.catalog.item.Item;
import com.ibm.pim.collaboration.CollaborationItem;
import com.ibm.pim.collaboration.ItemCollaborationArea;
import com.ibm.pim.collection.PIMCollection;
import com.ibm.pim.extensionpoints.WorkflowStepFunction;
import com.ibm.pim.extensionpoints.WorkflowStepFunctionArguments;
import com.ibm.pim.utils.Logger;

import es.ibm.santillana.mdm.common.bussinesmodel.ConfigModel;
import es.ibm.santillana.mdm.common.util.ConstSantillana;
import es.ibm.santillana.mdm.common.util.SantillanaLogger;
import es.ibm.santillana.mdm.integration.ItemChanges;
import es.ibm.santillana.mdm.state.StateManager;

/**
 * @author Benito
 *
 */
public class Success implements WorkflowStepFunction {

	/**
	 * 
	 */
	public Success() {
		// TODO Auto-generated constructor stub
	}

	/* (non-Javadoc)
	 * @see com.ibm.pim.extensionpoints.WorkflowStepFunction#in(com.ibm.pim.extensionpoints.WorkflowStepFunctionArguments)
	 */
	@Override
	public void in(WorkflowStepFunctionArguments arg0) {
		// TODO Auto-generated method stub
		Logger log = SantillanaLogger.getLogger();
		log.logInfo("IN del Success:");
		System.out.println("INnnnnn");
	}

	/* (non-Javadoc)
	 * @see com.ibm.pim.extensionpoints.WorkflowStepFunction#out(com.ibm.pim.extensionpoints.WorkflowStepFunctionArguments)
	 */
	@Override
	public void out(WorkflowStepFunctionArguments arg0) {
		Logger log = SantillanaLogger.getLogger();
		
		PIMCollection <CollaborationItem> collabItem = arg0.getItems();
		
		log.logInfo("Success out del item:");
		
		Iterator <CollaborationItem> it = collabItem.iterator();
		
		while (it.hasNext()) {
			log.logInfo("DENTRO DE ITERATORRR:");
			CollaborationItem collabitem = it.next();
			ItemCollaborationArea colArea = (ItemCollaborationArea) collabitem.getCollaborationArea();
			Catalog ctg = colArea.getSourceCatalog();
			Item itm = ctg.getItemByPrimaryKey(collabitem.getPrimaryKey());
			ItemChanges itChanges = new ItemChanges(itm,collabitem);
			Item itmCat = collabitem.getSourceItem();
			log.logInfo("REVISION out  del item: " + collabitem.getPrimaryKey());
			String country = ConfigModel.getCountryCodeFromColArea(collabitem.getCollaborationArea().getName());
			log.logInfo("REVISION out  country: " + country);
			ArrayList<String> specLocalList = ConfigModel.getSecondarySpecListFromSociedadesByCountry(collabitem, country);
			Iterator<String> itSpec = specLocalList.iterator();
			while (itSpec.hasNext()){
				String specName = itSpec.next();
				log.logInfo("REVISION out  specName: " + specName);
				StateManager.setState(collabitem, ConstSantillana.ESTADO_EN_LOGISTICA, specName);
				
				log.logInfo("Estado  del collab item: " + collabitem.getAttributeValue(specName+"/Estado"));
				
			}
			collabitem.save();
			itmCat.save();
	
			
		}
	}

	/* (non-Javadoc)
	 * @see com.ibm.pim.extensionpoints.WorkflowStepFunction#timeout(com.ibm.pim.extensionpoints.WorkflowStepFunctionArguments)
	 */
	@Override
	public void timeout(WorkflowStepFunctionArguments arg0) {
		// TODO Auto-generated method stub

	}

}
